// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.linqingying.gitcode.authentication

import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.util.concurrency.annotations.RequiresEdt
import org.jetbrains.annotations.CalledInAny
import com.linqingying.gitcode.authentication.accounts.GCAccountManager
import com.linqingying.gitcode.authentication.accounts.GitCodeAccount
import java.awt.Component


/**
 * Entry point for interactions with GitCode authentication subsystem
 */
@Service
@Deprecated("deprecated in favor of GCAccountsUtil")
class GitCodeAuthenticationManager internal constructor() {
  private val accountManager: GCAccountManager get() = service()

  @CalledInAny
  fun getAccounts(): Set<GitCodeAccount> = accountManager.accountsState.value

  @CalledInAny
  fun hasAccounts() = accountManager.accountsState.value.isNotEmpty()

  @RequiresEdt
  @JvmOverloads
  fun ensureHasAccounts(project: Project?, parentComponent: Component? = null): Boolean {
    if (accountManager.accountsState.value.isNotEmpty()) return true
    return GCAccountsUtil.requestNewAccount(project = project, parentComponent = parentComponent) != null
  }

  fun getSingleOrDefaultAccount(project: Project): GitCodeAccount? = GCAccountsUtil.getSingleOrDefaultAccount(project)

  companion object {
    @JvmStatic
    fun getInstance(): GitCodeAuthenticationManager = service()
  }
}
